﻿using System;
using System.Windows.Controls;
using System.Windows.Input;

namespace Webitel.Controls
{
	/// <summary>
	/// Логика взаимодействия для DialpadControl.xaml
	/// </summary>
	public partial class DialpadControl : UserControl
	{
		public DialpadControl()
		{
			InitializeComponent();
		}

		public delegate void DNDCheckEventHandler(bool value);

		public DNDCheckEventHandler DNDCheck;

		public bool IsDND
		{
			get
			{
				DNDCheck(DND.Checked);
				return DND.Checked;
			}
			set { DND.Checked = value; }
		}

		public delegate void MuteCheckEventHandler(bool value);

		public MuteCheckEventHandler MuteCheck;

		public bool IsMute
		{
			get
			{
				MuteCheck(Mute.Checked);
				return Mute.Checked;
			}
			set { Mute.Checked = value; }
		}

		public delegate void TransferCheckEventHandler(bool value);

		public TransferCheckEventHandler TransferCheck;

		public bool IsTransfer
		{
			get
			{
				TransferCheck(Transfer.Checked);
				return Transfer.Checked;
			}
			set { Transfer.Checked = value; }
		}

		string _lastNumber;
		
		public string Number
		{
			get { return DigitsPad.Number; }
			set { DigitsPad.Number = value; }
		}

		public bool IsNumberEmpty
		{
			get { return string.IsNullOrEmpty(Number.ToString()); }
		}
		
		private event MouseButtonEventHandler DClick;

		private void D_MouseDown(object sender, MouseButtonEventArgs e)
		{
			Number = Number + ((Webitel.Controls.DialpadButton)(sender)).Number;
			if (null != DClick)
			{
				DClick(sender, e);
			}
		}

		private void DigitsPad_MouseDown(object sender, MouseButtonEventArgs e)
		{
			switch (e.ChangedButton)
			{
				case MouseButton.Middle:
					Number = "";
					break;

				case MouseButton.Right:
					DeleteLastDigit();
					break;
			}
		}

		private void DeleteLastDigit()
		{
			if (Number.Length > 0)
			{
				Number = Number.Remove(Number.Length - 1);
			}
		}

		private event EventHandler DHoldClick;

		private event EventHandler DCallClick;

		private event EventHandler DTransferClick;

		private void DHold_MouseDown(object sender, MouseButtonEventArgs e)
		{
			if (null != DHoldClick)
			{
				DHoldClick(sender, e);
			}
		}

		private void DCall_MouseDown(object sender, MouseButtonEventArgs e)
		{
			OnCall(sender, e);			
		}

		private void OnCall(object sender, EventArgs e)
		{ 			
			if (/*null != DCallClick && */!IsNumberEmpty)
			{
				//DCallClick(sender, e);
				_lastNumber = Number;
				Number = "";
				return;
			}	
			if (true /* No active calls */ && IsNumberEmpty)
			{
				Number = _lastNumber;
			}
		}

		private void DTransfer_MouseDown(object sender, MouseButtonEventArgs e)
		{
			if (null != DTransferClick)
			{
				DTransferClick(sender, e);
			}
		}

		public void KeyPress(object sender, KeyEventArgs e)
		{
			switch (e.Key)
			{
				case Key.Escape:

					//1 Clear number
					if (!IsNumberEmpty)
					{
						Number = "";
					}
					break;

				case Key.Back:
					if (Number.Length > 0)
					{
						Number = Number.Remove(Number.Length - 1);
					}
					break;

				case Key.Enter:
					OnCall(sender, e);
					break;

				//1
				case Key.D1:
				case Key.NumPad1:
					Number += "1";
					break;

				//2
				case Key.A:
				case Key.B:
				case Key.C:
				case Key.D2:
				case Key.NumPad2:
					Number += "2";
					break;

				//3
				case Key.D:
				case Key.E:
				case Key.F:
				case Key.D3:
				case Key.NumPad3:
					Number += "3";
					break;

				//4
				case Key.G:
				case Key.H:
				case Key.I:
				case Key.D4:
				case Key.NumPad4:
					Number += "4";
					break;

				//5
				case Key.J:
				case Key.K:
				case Key.L:
				case Key.D5:
				case Key.NumPad5:
					Number += "5";
					break;

				//6
				case Key.M:
				case Key.N:
				case Key.O:
				case Key.D6:
				case Key.NumPad6:
					Number += "6";
					break;

				//7
				case Key.P:
				case Key.Q:
				case Key.R:
				case Key.S:
				case Key.D7:
				case Key.NumPad7:
					Number += "7";
					break;

				//8
				case Key.T:
				case Key.U:
				case Key.V:
				case Key.D8:
				case Key.NumPad8:
					Number += "8";
					break;

				//9
				case Key.W:
				case Key.X:
				case Key.Y:
				case Key.Z:
				case Key.D9:
				case Key.NumPad9:
					Number += "9";
					break;

				//0
				case Key.D0:
				case Key.NumPad0:
					Number += "0";
					break;

				//*
				case Key.Multiply:
					Number += "*";
					break;

				default:
					break;
			}
		}

		private void DND_MouseDown(object sender, MouseButtonEventArgs e)
		{
			DND.OnCheck();
		}

		private void Mute_MouseDown(object sender, MouseButtonEventArgs e)
		{
			Mute.OnCheck();
		}

		private void Transfer_MouseDown(object sender, MouseButtonEventArgs e)
		{
			Transfer.OnCheck();
		}
	}
}